package com.restaurant.application.exchangegoods;

import com.nebutown.base.ViewSpec;
import com.nebutown.base.data.View;
import com.nebutown.commonunits.utils.DateTimeUtils;
import com.nebutown.generation.basictype.UID;
import com.restaurant.application.accumulate.Accumulate;
import com.restaurant.application.accumulate.support.AccumulateData;
import com.restaurant.application.accumulategoods.AccumulateGoods;
import com.restaurant.application.accumulategoods.support.GoodsData;
import com.restaurant.application.accumulategoodscoupon.AccumulateGoodsCoupon;
import com.restaurant.application.customer.Customer;
import com.restaurant.application.customer.support.CustomerData;
import com.restaurant.application.exchangegoods.support.AbstractExchangeGoods;
import com.restaurant.application.indexes.exchangegoodslist.mysql.ExchangeGoodsListBean;
import com.restaurant.application.indexes.exchangegoodslist.mysql.ExchangeGoodsListImpl;

import java.util.Objects;

public class ExchangeGoodsImpl extends AbstractExchangeGoods {

    @Override
    public View getFullView() {
        return null;
    }

    @Override
    public View getViewLead(ViewSpec viewSpec) {
        return null;
    }

    @Override
    public String setExchangeGoodsData(UID customerUid, UID goodsUid , String type) {
        String result ="success";
        getData().setCustomerUID(customerUid);
        CustomerData miniCustomer = getUnit(Customer.class, customerUid).findData();
        getData().setCustomerId(miniCustomer.getId());
        getData().setNickName(miniCustomer.getNickName());
        getData().setPhone(miniCustomer.getPhone());
        getData().setGoodsUID(goodsUid);
        GoodsData goodsData = new GoodsData() ;
        // todo 兑换的规则
        // todo 1 等级是否够
        boolean aBoolean = whetherAccess(goodsUid,type);
        if(!aBoolean){
            return "等级不够";
        }
        if(Objects.equals("优惠券",type)){
            // 将优惠券绑定到用户的优惠券上
            AccumulateGoodsCoupon unit = getUnit(AccumulateGoodsCoupon.class, goodsUid);
            goodsData = unit.getGoodsData();
            if (goodsData.getAccumulatePoints() > miniCustomer.getUsableScores()) {
                return "积分不足";
            }
            String s = unit.exchangeCouponGoods();
            if (Objects.equals(s, "success")) {
                result = "兑换成功";
            }else{
                result = s;
                return result;
            }
        }else{
            AccumulateGoods unit = getUnit(AccumulateGoods.class, goodsUid);
            goodsData = unit.getGoodsData();
        }
        if (goodsData.getAccumulatePoints() > miniCustomer.getUsableScores()) {
            return "积分不足";
        }
        // 减掉积分
        getData().setGoodsName(goodsData.getGoodsName());
        getData().setAccumulate(goodsData.getAccumulatePoints());
        getData().setGoodsId(goodsData.getGoodsId());
        getData().setExchangeTime(DateTimeUtils.getCurrentMillisecond());
        insertBean();
        minusScores(customerUid,goodsData);
        return result;
    }

    private Boolean whetherAccess(UID goodsUid, String type) {
        return true;
    }

    private void minusScores(UID customerUid,GoodsData goodsData) {
        getUnit(Customer.class,customerUid).minusScores(goodsData.getAccumulatePoints().intValue());
        createNode(Accumulate.class).init(new AccumulateData().withAccumulate(goodsData.getAccumulatePoints())
                .withAction("兑换商品"+goodsData.getGoodsName()).withDetail(getData().getCouponUID())
                .withType(Accumulate.ACCUMULATE_DECREACE_EXCHANGE));
    }

    private ExchangeGoodsListBean insertBean() {
        ExchangeGoodsListBean bean =new ExchangeGoodsListBean().withCreateTime(getData().getExchangeTime())
                .withAccumulate(getData().getAccumulate()).withGoodsId(getData().getGoodsId())
                .withGoodsName(getData().getGoodsName()).withCustomerId(getData().getCustomerId())
                .withNickName(getData().getNickName()).withPhone(getData().getPhone());
        getIndex(ExchangeGoodsListImpl.class).insert(bean);
        return bean;
    }
}
